package com.nondo.dean.product.common;

import com.nondo.dean.common.tool.AliyunOssTool;
import com.nondo.dean.product.domain.ProductCategory;
import com.nondo.dean.product.model.CategoryBean;
import com.nondo.dean.product.model.result.CategoryResult;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * project：dean-cloud
 *
 * @author linfeng @ nondo
 * @date 2018/5/31
 */
public class CategoryUtils {


    public static List<CategoryResult> handle(List<ProductCategory> allCategoryList,
        List<ProductCategory> secondCategoryList, AliyunOssTool aliyunOssTool) {

        List<CategoryResult> categoryResultList = new ArrayList<>();

        if (allCategoryList == null || allCategoryList.isEmpty()) {
            return categoryResultList;
        }

        if (secondCategoryList == null || secondCategoryList.isEmpty()) {
            return categoryResultList;
        }

        List<Integer> secondCategoryIds =
            secondCategoryList.parallelStream().map(ProductCategory::getId).collect(Collectors.toList());

        CategoryResult categoryResult;
        CategoryBean categoryBean;

        Iterator<ProductCategory> iter = allCategoryList.iterator();
        while (iter.hasNext()) {
            ProductCategory item = iter.next();
            if (item.getPid() == 0 || item.getPid() == null) {

                categoryResult = new CategoryResult();
                categoryResult.setId(item.getId());
                categoryResult.setImage(aliyunOssTool.getImgUrl(item.getImage()));
                categoryResult.setName(item.getName());
                categoryResult.setOrderNum(item.getOrderNum());
                categoryResultList.add(categoryResult);

                iter.remove();
            }
        }

        for (ProductCategory category : allCategoryList) {
            if (secondCategoryIds.contains(category.getId())) {
                categoryBean = new CategoryBean();
                categoryBean.setId(category.getId());
                categoryBean.setImage(aliyunOssTool.getImgUrl(category.getImage()));
                categoryBean.setName(category.getName());
                categoryBean.setOrderNum(category.getOrderNum());
                categoryBean.setPid(category.getPid());
                treeCategory(categoryResultList, categoryBean);
            }
        }
        Iterator<CategoryResult> resultIterator = categoryResultList.iterator();
        while (resultIterator.hasNext()) {
            CategoryResult result = resultIterator.next();
            if (result.getChildrenList().isEmpty()) {
                resultIterator.remove();
            }
        }

        return categoryResultList;

    }

    private static void treeCategory(List<CategoryResult> categoryResultList, CategoryBean categoryBean) {

        for (CategoryResult categoryResult : categoryResultList) {
            if (categoryResult.getId().equals(categoryBean.getPid())) {
                categoryResult.getChildrenList().add(categoryBean);
            }
        }
    }

}
